package com.zds.scm.dao.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.ClassInfoDAO;
import com.zds.scm.domain.ClassInfo;
import com.zds.scm.domain.DeptInfo;

public class ClassInfoDAOImpl extends AbstractDAOImpl<ClassInfo> implements ClassInfoDAO {
	@Override
	protected ClassInfo rowMapper(ResultSet rs) throws SQLException {
		ClassInfo classInfo=new ClassInfo();
		try{
			DeptInfo deptInfo = new DeptInfo();
			deptInfo.setDeptCode(rs.getString("Dept_Code"));
			deptInfo.setDeptName(rs.getString("Dept_Name"));
			deptInfo.setDeptDesc(rs.getString("Dept_Desc"));
			
			classInfo.setClassCode(rs.getString("Class_Code"));
			classInfo.setClassName(rs.getString("Class_Name"));
			classInfo.setDeptInfo(deptInfo);
		} catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
			logger.error("DAO层结果集映射处理错误:"+e.getMessage());
			throw new DaoException(e.getMessage(),e);
		}
		return classInfo;
	}
	@Override
	public boolean delete(ClassInfo classInfo) {
		String sql="delete from classinfo where class_code=?";
		Object[] args=new Object[]{classInfo.getClassCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean insert(ClassInfo classInfo) {
		String sql="insert into classinfo(Dept_Code,Class_Name,Class_Code) values(?,?,?)";
		Object[] args=new Object[]{classInfo.getDeptInfo().getDeptCode(),classInfo.getClassName(),classInfo.getClassCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean update(ClassInfo classInfo) {
		String sql="update classinfo set Dept_Code=?,Class_Name=? where Class_Code=?";
		Object[] args=new Object[]{classInfo.getDeptInfo().getDeptCode(),classInfo.getClassName(),classInfo.getClassCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public List<ClassInfo> findAll() {
		return findAll(-1,-1);
	}
	@Override
	public List<ClassInfo> findAll(int currentPage, int pageSize){
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptinfo b where a.dept_code=b.dept_code order by a.class_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRows(){
		String sql="select count(*) from classinfo a,deptinfo b where a.dept_code=b.dept_code";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	@Override
	public ClassInfo findById(Serializable id) {
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptinfo b where a.dept_code=b.dept_code and a.class_code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}

	@Override
	public List<ClassInfo> findByDeptCode(String DeptCode,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.dept_code=? order by a.class_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{DeptCode};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{DeptCode,(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsDeptCode(String DeptCode){
		String sql="select count(*) from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.dept_code=?";
		Object[] args=new Object[]{DeptCode};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<ClassInfo> findByName(String likeClassName,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_name like ? order by a.class_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeClassName+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeClassName+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
    @Override
    public int getTotalRowsByName(String likeClassName){
    	String sql="select count(*) from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_name like ? order by a.class_code";
		Object[] args=new Object[]{"%"+likeClassName+"%"};
		return super.findEntityCount(sql, args);
    }
	@Override
	public List<ClassInfo> findByCode(String likeClassCode,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_code like ? order by a.class_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeClassCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeClassCode+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByCode(String likeClassCode){
		String sql="select count(*) from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_code like ?";
		Object[] args=new Object[]{"%"+likeClassCode+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<ClassInfo> findByNameAndCode(String likeClassCode,String likeClassName, String likeDeptCode, int currentPage,int pageSize) {
		String sql="select a.*,b.dept_Name,b.dept_desc from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_code like ? and a.class_name like ? and a.dept_code like ? order by a.class_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeClassCode+"%","%"+likeClassName+"%","%"+likeDeptCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeClassCode+"%","%"+likeClassName+"%","%"+likeDeptCode+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByNameAndCode(String likeClassCode,String likeClassName, String likeDeptCode) {
		String sql="select count(*) from classinfo a,deptInfo b where a.dept_code=b.dept_code and a.class_code like ? and a.class_name like ? and a.dept_code like ?";
		Object[] args=new Object[]{"%"+likeClassCode+"%","%"+likeClassName+"%","%"+likeDeptCode+"%"};
		return super.findEntityCount(sql, args);
	}
}
